package com.fingard.dsp.bank.directbank.vbao03;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IContract;
import com.fingard.dsp.bank.directbank.vbao03.request.ContractApplyRequest;
import com.fingard.dsp.bank.directbank.vbao03.request.ContractQueryRequest;
import com.fingard.dsp.bank.directbank.vbao03.request.ContractQueryRequestDetail;
import com.fingard.dsp.bank.reqrespobj.contract.*;
import com.fingard.dsp.bank.reqrespobj.contract.detail.RetAtsContractDetail;
import com.fingard.text.StringHelper;

import java.util.ArrayList;
import java.util.List;

public class VBAO03Contract extends VBAO03Base implements IContract {
    @Override
    public RetAtsContract contract(ReqContract p_req) throws Exception {
        RetAtsContract retAts = new RetAtsContract(p_req.reqHeader);
        RetAtsContractDetail tmpRetDetail = new RetAtsContractDetail();
        tmpRetDetail.reqSeqID = p_req.contractDetail.askNumber;
        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req.contractDetail.recAct, "");
            if (actItem == null) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = "未找到账号:[" + p_req.contractDetail.payAct + "]商户配置信息!";
                tmpRetDetail.reqSeqID = p_req.contractDetail.askNumber;
                tmpRetDetail.signState = ContractState.FAIL;
                retAts.writeRetDetail(tmpRetDetail);
                return retAts;
            }
            ContractApplyRequest applyRequest = new ContractApplyRequest();
            if ("01".equals(p_req.contractDetail.step)) {
                applyRequest.setOrderNo(p_req.contractDetail.askNumber);
                applyRequest.setDataSource("");
                applyRequest.setReqType("");
                String bankCode = getBankConfig().getMapValueByKey("BankCodeMapping", p_req.contractDetail.payBankCode);
                if (StringHelper.isNullOrEmpty(bankCode)) {
                    retAts.writeRespCode("0");
                    retAts.writeRespInfo("未找到BankCodeMapping中对应" + p_req.contractDetail.payBankCode + "配置信息,请检查config.xml相关配置!");
                    retAts.writeEnd();
                    return retAts;
                }
                applyRequest.setChannelNo(bankCode);
                applyRequest.setBankCode(bankCode);
                applyRequest.setCardName(p_req.contractDetail.payName);
                applyRequest.setCardNo(p_req.contractDetail.payAct);
                applyRequest.setMobile(p_req.contractDetail.cellPhone);
                applyRequest.setCertNo(p_req.contractDetail.certNum);
                applyRequest.setCertType(convertCertTypeToBank(p_req.contractDetail.certType));
                if ("1".equals(p_req.contractDetail.isPrivate)) {
                    applyRequest.setAccountType("private");
                } else {
                    applyRequest.setAccountType("public");
                }
                if ("1".equals(p_req.contractDetail.cardType)) {
                    applyRequest.setCardType("book");
                } else if ("2".equals(p_req.contractDetail.cardType)) {
                    applyRequest.setCardType("debit");
                } else if ("3".equals(p_req.contractDetail.cardType)) {
                    applyRequest.setCardType("credit");
                }
                applyRequest.setCreditCvv2("");
                applyRequest.setCreditValid("");
            } else if ("02".equals(p_req.contractDetail.step)) {
                applyRequest.setOrderNo(p_req.reqHeader.transSeqID);
                applyRequest.setDataSource(bankFront.corpNo);
                applyRequest.setOriginOrderNo(p_req.contractDetail.askNumber);
                applyRequest.setVerifyCode(p_req.contractDetail.smsCode);
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "未能识别的step：" + p_req.contractDetail.step;
                retAts.writeRetDetail(tmpRetDetail);
                return retAts;
            }
            String url = "";
            if ("01".equals(p_req.contractDetail.step)) {
                url = bankFront.getUrl("contractApply");
            } else if ("02".equals(p_req.contractDetail.step)) {
                url = bankFront.getUrl("contractConfirm");
            }
            JSONObject jsonObjectReturn = sendToBank(JSONObject.toJSONString(applyRequest), actItem, url);

            if (!"".equals(jsonObjectReturn) && jsonObjectReturn != null) {
                JSONObject respBizContent = decryptByRSA(jsonObjectReturn, actItem);
                String respCode = respBizContent.getString("returnCode");
                String tmpRespInfo = respBizContent.getString("returnMsg");
                if (respCode != null) {
                    if ("01".equals(p_req.contractDetail.step)) {
                        tmpRetDetail.respCode = "0";
                        tmpRetDetail.respInfo = tmpRespInfo;
                    } else if ("02".equals(p_req.contractDetail.step)) {
                        tmpRetDetail.respCode = "0";
                        /**
                         * 1 签约成功  * 2 签约失败 * 3 签约中 * 4 未签约
                         */
                        String signState = respBizContent.getString("contractStatus");
                        if ("1".equals(signState)) {
                            tmpRetDetail.signState = ContractState.SUCCESS;
                        } else if ("2".equals(signState)) {
                            tmpRetDetail.signState = ContractState.FAIL;
                        } else if ("3".equals(signState)) {
                            tmpRetDetail.signState = ContractState.SIGNING;
                        } else if ("4".equals(signState)) {
                            tmpRetDetail.signState = ContractState.UNSIGNED;
                        }
                        tmpRetDetail.authCode = respBizContent.getString("contractNo");
                        tmpRetDetail.respInfo = tmpRespInfo;
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpRespInfo;
                }
                tmpRetDetail.unifiedCode = getBankConfig().getMapValueByKey("contractMapping", respCode);
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "银行响应内容为空,请联系银行咨询!";
                tmpRetDetail.signState = ContractState.FAIL;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsContract upContract(ReqContract p_req) throws Exception {
        return null;
    }

    @Override
    public RetAtsContract qryContract(ReqContract p_reqQry) throws Exception {
        RetAtsContract retAts = new RetAtsContract(p_reqQry.reqHeader);
        RetAtsContractDetail tmpRetDetail = new RetAtsContractDetail();
        tmpRetDetail.reqSeqID = p_reqQry.contractDetail.askNumber;

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_reqQry.contractDetail.recAct, "");
            if (actItem == null) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = "未找到账号:[" + p_reqQry.contractDetail.payAct + "]商户配置信息!";
                tmpRetDetail.reqSeqID = p_reqQry.contractDetail.askNumber;
                tmpRetDetail.signState = ContractState.FAIL;
                retAts.writeRetDetail(tmpRetDetail);
                return retAts;
            }
            List<ContractQueryRequestDetail> queryRequestDetailList = new ArrayList<ContractQueryRequestDetail>();
            ContractQueryRequestDetail detail = new ContractQueryRequestDetail();
            detail.setItemNo("1");
            detail.setCardName(p_reqQry.contractDetail.payName);
            detail.setCardNo(p_reqQry.contractDetail.payAct);
            detail.setMobile(p_reqQry.contractDetail.cellPhone);
            detail.setCertNo(p_reqQry.contractDetail.certNum);
            detail.setCertType(convertCertTypeToBank(p_reqQry.contractDetail.certType));
            queryRequestDetailList.add(detail);
            ContractQueryRequest contractQueryRequest = new ContractQueryRequest();
            contractQueryRequest.setList(queryRequestDetailList);
            contractQueryRequest.setAppId(actItem.appId);
            String channelNo = getBankConfig().getMapValueByKey("BankCodeMapping", p_reqQry.contractDetail.payBankCode);
            if (StringHelper.isNullOrEmpty(channelNo)) {
                retAts.writeRespCode("0");
                retAts.writeRespInfo("未找到BankCodeMapping中对应" + p_reqQry.contractDetail.payBankCode + "配置信息,请检查config.xml相关配置!");
                retAts.writeEnd();
                return retAts;
            }
            contractQueryRequest.setChannelNo(channelNo);

            JSONObject jsonObjectReturn = sendToBank(JSONObject.toJSONString(contractQueryRequest), actItem, bankFront.getUrl("qryContract"));
            if (!"".equals(jsonObjectReturn) && jsonObjectReturn != null) {
                JSONObject listObject = decryptByRSA(jsonObjectReturn, actItem);
                String respCode = listObject.getString("returnCode");
                String tmpRespInfo = listObject.getString("returnMsg");
                if (respCode != null) {
                    JSONArray respBizContents = listObject.getJSONArray("list");
                    JSONObject respBizContent = (JSONObject) respBizContents.get(0);
                    tmpRetDetail.respCode = "0";
                    tmpRetDetail.payName = respBizContent.getString("cardName");
                    tmpRetDetail.payAct = respBizContent.getString("cardNo");
                    tmpRetDetail.authCode = respBizContent.getString("contractNo");
                    tmpRetDetail.certNum = respBizContent.getString("certNo");
                    tmpRetDetail.certType = respBizContent.getString("certType");
                    String signState = respBizContent.getString("status");
                    //协议状态 1-生效 2-失效
                    if ("1".equals(signState)) {
                        tmpRetDetail.signState = ContractState.SUCCESS;
                    } else if ("2".equals(signState)) {
                        tmpRetDetail.signState = ContractState.FAIL;
                    } else if ("3".equals(signState)) {
                        tmpRetDetail.signState = ContractState.SIGNING;
                    } else if ("4".equals(signState)) {
                        tmpRetDetail.signState = ContractState.UNSIGNED;
                    }
                    tmpRetDetail.respInfo = tmpRespInfo;
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpRespInfo;
                }
                tmpRetDetail.unifiedCode = getBankConfig().getMapValueByKey("contractMapping", respCode);
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "银行响应内容为空,请联系银行咨询!";
                tmpRetDetail.signState = ContractState.FAIL;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;

    }

    @Override
    public RetAtsQryBatContract qryBatContract(ReqQryBatContract p_reqQry) throws Exception {
        return null;
    }
}
